package com.example.administrator.daydayappmarket.software.rating.fragment.newrating.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.software.choice.activity.SoftwareChoiceSecondActivity;
import com.example.administrator.daydayappmarket.software.choice.model.SoftwareChoiceTopicApps;
import com.squareup.picasso.Picasso;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by lenovo on 2016/12/27.
 */

public class SoftwareRatingNewAdapter extends BaseAdapter {

    private Context context;
    private List<SoftwareChoiceTopicApps> data;

    public SoftwareRatingNewAdapter(Context context, List<SoftwareChoiceTopicApps> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public SoftwareChoiceTopicApps getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_software_rating_new_listview, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        int num = position + 4;
        viewHolder.itemSoftwareRatingListViewNumText.setText("" + num);

        final SoftwareChoiceTopicApps softwareChoiceTopicApps = data.get(position);

        viewHolder.itemSoftwareRatingListViewNameText.setText(softwareChoiceTopicApps.getName());

        int download_count = softwareChoiceTopicApps.getDownload_count();
        viewHolder.itemSoftwareRatingListViewPersonText.setText(download_count / 10000 + " 万人下载");

        double v = (double) softwareChoiceTopicApps.getPackage_size() / 1024 / 1024;
        DecimalFormat df = new DecimalFormat("####0.00");
        String format = df.format(v);
        viewHolder.itemSoftwareRatingListViewSizeText.setText(format + "MB");

        viewHolder.itemSoftwareRatingListViewReviewText.setText(softwareChoiceTopicApps.getReviews());

        Picasso.with(context).load(softwareChoiceTopicApps.getIcon_url()).into(viewHolder.itemSoftwareRatingListViewImageView);

        viewHolder.itemSoftwareRatingListViewRelative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String id = softwareChoiceTopicApps.getId();
                Intent intent=new Intent(context,SoftwareChoiceSecondActivity.class);
                intent.putExtra("id",id);
                context.startActivity(intent);
            }
        });

        return convertView;
    }



    static class ViewHolder {
        @InjectView(R.id.item_software_rating_listView_numText)
        TextView itemSoftwareRatingListViewNumText;
        @InjectView(R.id.item_software_rating_listView_imageView)
        ImageView itemSoftwareRatingListViewImageView;
        @InjectView(R.id.item_software_rating_listView_nameText)
        TextView itemSoftwareRatingListViewNameText;
        @InjectView(R.id.item_software_rating_listView_reviewText)
        TextView itemSoftwareRatingListViewReviewText;
        @InjectView(R.id.item_software_rating_listView_personText)
        TextView itemSoftwareRatingListViewPersonText;
        @InjectView(R.id.item_software_rating_listView_sizeText)
        TextView itemSoftwareRatingListViewSizeText;
        @InjectView(R.id.item_software_rating_listView_button)
        TextView itemSoftwareRatingListViewButton;//将Button更改为TextView
        @InjectView(R.id.item_software_rating_listView_relative)
        RelativeLayout itemSoftwareRatingListViewRelative;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }


}
